Ext.define('BenBen.modules.system.UserModule',{
    extend : 'Ext.ux.desktop.Module',
    requires: [
        'BenBen.common.BasePanel',
        'BenBen.common.component.TreePicker'
    ],
    id : 'user-win',
    border : false,
    init : function(){
        this.launcher = {
            text: '用户管理',
            iconCls:'icon-grid'
        };
    },
    createWindow : function() {
        var desktop = this.app.getDesktop();
        var win = desktop.getWindow('user-win');

        if (!win) {
            var dataModelFields = ['_id','username','fullName','department.departmentName','age','sex','address','phone','email','status'];

            var columns = [ {
                xtype: 'rownumberer',
                width: 40,
                sortable: false,
                locked: true
            },{
                dataIndex : 'username',
                text : '用户名'
            },{
                dataIndex : 'fullName',
                text : '姓名'
            },{
                dataIndex: 'department.departmentName',
                text: '部门'
            },{
                dataIndex : 'age',
                text : '年龄'
            },{
                dataIndex : 'sex',
                text : '性别'
            },{
                dataIndex : 'address',
                text : '联系地址'
            },{
                dataIndex : 'phone',
                text : '联系电话'
            },{
                dataIndex : 'email',
                text : '邮箱'
            },{
                dataIndex : 'status',
                text : '状态',
                renderer: function (value) {
                    if (value == '1') {
                        return "正常";
                    } else if (value == '2') {
                        return "停用";
                    } else if (value == '3') {
                        return "离职";
                    }
                }
            }];

            Ext.define('model.user', {
                extend : 'Ext.data.Model',
                fields : [{
                    name : 'username'
                },{
                    name : 'password'
                },{
                    name : 'fullName'
                },{
                    name : 'age'
                },{
                    name : 'sex'
                },{
                    name : 'address'
                },{
                    name : 'phone'
                },{
                    name : 'email'
                },{
                    name : 'status'
                }]
            });

            var searchItem = [{
                xtype: 'treepicker',
                fieldLabel: '部门',
                name: 'department',
                isSearch : true,
                value : '',
                idField : '_id',
                displayField: 'departmentName',
                sumbitDisplayField : true,
                minPickerHeight: 200,
                store: Ext.create('Ext.data.TreeStore',{
                    fields: ['_id','departmentName'],
                    root: {
                        departmentName: 'root',
                        expanded: true
                    },
                    proxy: {
                        type: 'rest',
                        method : 'GET',
                        url: '/systemDepartment/tree',
                        reader: {
                            type: 'json'
                        }
                    }
                })
            },{
                xtype : 'textfield',
                name : 'username',
                isSearch : true,
                fieldLabel : '用户名',
                labelAlign : 'right'
            },{
                xtype : 'textfield',
                name : 'fullName',
                isSearch : true,
                fieldLabel : '姓名',
                labelAlign : 'right'
            }];


            var dataForm = Ext.create('Ext.form.Panel', {
                border : false,
                bodyStyle : 'padding:5px',
                layout : 'anchor',
                defaults : {
                    layout : 'column',
                    margin: '5 5 5 5',
                    anchor : '90%',
                    labelWidth : 60,
                    msgTarget :'side',
                    labelAlign : 'right'
                },
                reader : Ext.create('Ext.data.JsonReader', {
                    root : 'data',
                    model : 'model.user'
                }),
                items : [{
                    xtype     : 'textfield',
                    fieldLabel: '用户名',
                    allowBlank: false,
                    blankText :'姓名不能为空',
                    name      :'username'
                },{
                    xtype : 'textfield',
                    fieldLabel : '姓名',
                    name : 'fullName',
                    allowBlank: false,
                    blankText : '姓名不能为空',
                    labelAlign : 'right'
                }, {
                    xtype: 'treepicker',
                    fieldLabel: '部门',
                    allowBlank: false,
                    blankText : '部门不能为空',
                    name: 'department',
                    value : '',
                    idField : '_id',
                    displayField: 'departmentName',
                    sumbitDisplayField : true,
                    store: Ext.create('Ext.data.TreeStore',{
                        fields: ['_id','departmentName'],
                        root: {
                            departmentName: 'root',
                            expanded: true
                        },
                        proxy: {
                            type: 'rest',
                            method : 'GET',
                            url: '/systemDepartment/tree',
                            reader: {
                                type: 'json'
                            }
                        }
                    })
                },{
                    xtype : 'textfield',
                    fieldLabel : '年龄',
                    name : 'age',
                    labelAlign : 'right'
                },{
                    xtype      : 'fieldcontainer',
                    fieldLabel : '性别',
                    defaultType: 'radiofield',
                    defaults   : {
                        flex:1
                    },
                    layout: 'hbox',
                    items: [{
                        boxLabel  : '男',
                        name      : 'sex',
                        inputValue: '男',
                        checked   : true,
                        id        : 'radio1'
                    }, {
                        boxLabel  : '女',
                        name      : 'sex',
                        inputValue: '女',
                        id        : 'radio2'
                    }]
                },{
                    xtype : 'textfield',
                    name : 'address',
                    fieldLabel : '联系地址',
                    labelAlign : 'right'
                },{
                    xtype : 'textfield',
                    name : 'phone',
                    fieldLabel : '联系电话',
                    labelAlign : 'right'
                },{
                    xtype : 'textfield',
                    name : 'email',
                    fieldLabel : '邮箱',
                    labelAlign : 'right'
                }]
            });

            var userPanel = Ext.create('BenBen.common.BasePanel', {
                columns : columns,
                searchItem : searchItem,
                dataForm : dataForm,
                dataModelFields : dataModelFields,
                tbarDefaultItems : 'add,update,look',
                config : {
                    url : '/systemUser/users',
                    pageSize : 30,
                    winWidth : 300,
                    winHeigth : 290
                }
            });

            win = desktop.createWindow({
                id: 'user-win',
                title: '用户管理',
                width: 800,
                height: 480,
                iconCls: 'icon-grid',
                animCollapse: false,
                constrainHeader: true,
                layout: 'fit',
                items: [userPanel]
            });
        }

        return win;
    }
});
